require(["core", "hash_routie", "coAjax"], function(jSouper, hash_routie, coAjax) {
	/*
	 * 真理权限数据
	 */
	var _pers_name_map = {};
	_pers_name_map["业务管理"] = {
		"orders|order": "订单管理",
		"shelves|goodsmanager|goodsmanage|update_goods|goodsupdate": "商品管理",
		"tile": "分类管理",
		// "space": "桌座管理",
		"evaluation|eva": "评价管理",
		// "set_goods_call_back_buff": "支付回调",
		"express": "运费模板"
	};
	_pers_name_map["营销管理"] = {
		// "qrcode": "二 维 码",
		// "coupon": "优惠促销",
		// "bus_user": "提现管理",
		// "points": "推荐返利",
		// "shoppinguide": "推荐返利·员工提成",
		// "bina": "分销管理",
		"visitorinfo|visitor": "访客信息",
		// "viplist": "会员列表",
		// "sendmail": "信息群发",
		// "assshop": "店铺关联",
		// "assshop-vcode": "店铺关联·申请码"
	};
	_pers_name_map["商铺管理"] = {
		"storeinfo|changepwd|info": "本站信息",
		"setmap": "地图标注",
		// "themepackage|themeimg|themecolor|theme": "网站风格",
		"mainset|noticeset": "广告设置",
		// "business": "业务开通",
		// "invite|invite-bus": "推荐建站",
		"permission": "子 账 号"
	};
	var pers_name_map = jSouper.extend({},
		_pers_name_map["业务管理"],
		_pers_name_map["营销管理"],
		_pers_name_map["商铺管理"]);
	var pers_name_arr = [];
	var pers_key_arr = [];
	var _pers_name_arr = {};
	jSouper.forEach(_pers_name_map, function(_pers_name_map_item, type) {
		var _pers_name_arr_item = _pers_name_arr[type] = []
		jSouper.forEach(_pers_name_map_item, function(value, key) {
			var item = {
				key: key,
				value: value
			};
			_pers_name_arr_item.push(item);
			pers_name_arr.push(item);
			pers_key_arr.push(key);
		});
		App.set("$Cache.bus.permission.pers_name_map_by_type." + type, _pers_name_arr_item);
	});
	App.set("$Cache.bus.permission.pers_name_map", pers_name_map);
	App.set("$Cache.bus.permission.pers_name_arr", pers_name_arr);
	App.set("$Cache.bus.permission.pers_key_arr", pers_key_arr);
	/*
	 * 获取列表
	 */
	hash_routie.on("permission", function() {
		coAjax.get(appConfig.bus.employees_list, function(result) {
			App.set("$Cache.bus.permission.bus_child_list", result.result);
		});
	});

	/*编辑子账号*/
	//启动编辑
	App.set("$Event.bus.permission.edit_info", function(e, vm) {
		App.set("$Cache.bus.edit_permission", vm.get());
	});
	//新账号
	App.set("$Event.bus.edit_permission.new", function() {
		App.set("$Cache.bus.edit_permission", {});
	});
	//修改权限
	App.set("$Event.bus.edit_permission.change_perms", function() {
		var perms_map = App.get("$Cache.bus.edit_permission.perms_map");
		var perms_arr = App.get("$Cache.bus.edit_permission.perms") || [];
		jSouper.forEach(perms_map, function(checked, key) {
			if (checked) {
				perms_arr.indexOf(key) === -1 && perms_arr.push(key)
			} else {
				var index = perms_arr.indexOf(key);
				if (index !== -1) {
					perms_arr.splice(index, 1)
				}
			}
		});
		console.dir(perms_map);
		console.log(perms_arr);
		App.set("$Cache.bus.edit_permission.perms", perms_arr);
	});
	//提交编辑
	App.set("$Event.bus.edit_permission.lock", function() {
		var edit_permission = App.get("$Cache.bus.edit_permission");
		/*提交前的数据过滤，删除无用的权限项，可能是过期的数据*/
		var _new_perms = [];
		jSouper.forEach(edit_permission.perms, function(permission_key) {
			if (pers_key_arr.indexOf(permission_key) !== -1) {
				_new_perms.push(permission_key)
			}
		});
		edit_permission.perms = _new_perms;

		if (edit_permission._id) {
			coAjax.put(appConfig.bus.update_employees + edit_permission._id, edit_permission, function(result) {
				alert("success", "修改子账号成功");
				jSouper.forEach(App.get("$Cache.bus.permission.bus_child_list"), function(item, index) {
					if (item._id == edit_permission._id) {
						App.set("$Cache.bus.permission.bus_child_list." + index, result.result);
					}
				});
			});
		} else {
			coAjax.post(appConfig.bus.add_employees, edit_permission, function(result) {
				alert("success", "添加子账号成功");
				App.get("$Event.bus.edit_permission.new")();
				App.push("$Cache.bus.permission.bus_child_list", result.result);
			});
		}
	});
});